package com.longshihan.news.ui.main.activity;

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.longshihan.news.R;
import com.longshihan.news.base.BaseActivity;
import com.longshihan.news.presenter.MainPresenter;
import com.longshihan.news.presenter.contract.MainContract;
import com.longshihan.news.ui.gank.fragment.GankMainFragment;
import com.longshihan.news.ui.juhe.fragment.JuHeMainFragment;
import com.longshihan.news.ui.main.fragment.SettingFragment;
import com.longshihan.news.ui.zhihu.fragment.ZhihuMainFragment;
import com.longshihan.news.util.ZToast;

import butterknife.BindView;
import me.yokeyword.fragmentation.SupportFragment;


public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View,NavigationView.OnNavigationItemSelectedListener  {

    @BindView(R.id.tb_main)
    Toolbar mToolBar;


    private static final String ITEM_ZHIHU = "知乎日报";
    private static final String ITEM_GANK  = "干货集中营";
    public static final String ITEM_JUHE = "聚合数据";
    public static final String ITEM_SETTING = "设置";
    ZhihuMainFragment mZhihuMainFragment;
    private FragmentManager  fragmentManager;
    private GankMainFragment mGankMainFragment;
    private JuHeMainFragment mJuHeMainFragment;
    private SupportFragment mCurrentFragment;
    private SettingFragment mSettingFragment;


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_includeactivity;
    }

    @Override
    protected void initEventAndData() {
        setToolBar(mToolBar, ITEM_ZHIHU);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        //安全考虑密码不会明文存储，而是姓名（或其他）与密码的md5值
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, mToolBar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        fragmentManager = getSupportFragmentManager();
        mZhihuMainFragment = new ZhihuMainFragment();
        mGankMainFragment = new GankMainFragment();
        mJuHeMainFragment = new JuHeMainFragment();
        mSettingFragment = new SettingFragment();
        mCurrentFragment = mZhihuMainFragment;
        loadMultipleRootFragment(R.id.fl_main_content, 0, mZhihuMainFragment, mGankMainFragment,mJuHeMainFragment,mSettingFragment);
    }

    @Override
    public void showError(String msg) {
        ZToast.showShortToast(mContext,msg);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_zhihu) {
            if (mCurrentFragment == mZhihuMainFragment) {
                return false;
            }
            showHideFragment(mZhihuMainFragment, mCurrentFragment);
            setToolBar(mToolBar,ITEM_ZHIHU);
            mCurrentFragment = mZhihuMainFragment;
        } else if (id == R.id.nav_meizhi) {
            if (mCurrentFragment == mGankMainFragment) {
                return false;
            }
            showHideFragment(mGankMainFragment, mCurrentFragment);
            setToolBar(mToolBar,ITEM_GANK);
            mCurrentFragment = mGankMainFragment;
        } else if (id == R.id.nav_juhe) {
            if (mCurrentFragment == mJuHeMainFragment) {
                return false;
            }
            showHideFragment(mJuHeMainFragment, mCurrentFragment);
            setToolBar(mToolBar,ITEM_JUHE);
            mCurrentFragment = mJuHeMainFragment;
        } else if (id == R.id.nav_collection) {

        } else if (id == R.id.nav_setting) {
            if (mCurrentFragment == mSettingFragment) {
                return false;
            }
            showHideFragment(mSettingFragment, mCurrentFragment);
            setToolBar(mToolBar,ITEM_SETTING);
            mCurrentFragment = mSettingFragment;
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
